/*
 * randomtestcard.c
 *
 *  Created on: Feb 8, 2014
 *      Author: Laurye_OSU
 */

#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <math.h>

#define NOISY_TEST 1

int checkSalvagerCard(int p, int handPos, int choice1, int choice2, int choice3, struct gameState *post) {
  int r;
  int bonus=0;
  int errornum=0;
  int temphand[MAX_HAND];
  int starthand[MAX_HAND];

  //what is my card count for the real function starting?
   // starthand[p] = post->handCount[p];
   // printf("starting hand count for real function %d\n", starthand[p]);


  //make copy before calling the real function
  struct gameState pre;
  memcpy(&pre, post, sizeof(struct gameState)); //make a copy of the real game state into pre

  //what is my card count starting?
  //  temphand[p] = pre.handCount[p];
  //  printf("starting hand count for test hand %d\n", temphand[p]);


 // printf("choice 1 is %d\n", choice1);

  //call the real function
  //int cardEffect(int card, int choice1, int choice2, int choice3,
  //struct gameState *state, int handPos, int *bonus)
  r = cardEffect(salvager, choice1, 0, 0, post, handPos, bonus);

  //Code is from dominion.c
  //+1 buy
  pre.numBuys++;

   if (choice1)
  	{
  	  //gain coins equal to trashed card
  	  pre.coins = pre.coins + getCost( handCard(choice1, &pre) );
  	  //trash card
  	  discardCard(choice1, p, &pre, 1);
  	}

   //discard card
   discardCard(handPos, p, &pre, 0);

/*
        //what are my hand counts after adventurer. I should have 1 more card because I have to discard
          	  printf("real function hand count %d\n", post->handCount[p]);
          	  printf("test function hand count %d\n", pre.handCount[p]);

       //what are my hand counts after adventurer. I should have 1 more card because I have to discard
           	  printf("real function coins %d\n", post->coins);
          	  printf("test function coins %d\n", pre.coins);

      //what are my hand counts after adventurer. I should have 1 more card because I have to discard
           	  printf("real function deck count %d\n", post->deckCount[p]);
           	  printf("test function deck count %d\n", pre.deckCount[p]);

 */
   //compare test with the actual function
 if (memcmp(&pre, post, sizeof(struct gameState)) != 0){
    errornum++;
    printf("Test failed because real function did not match the test results: %d\n", errornum);
 }

   return 0;

}



      int main() {
      	int i;
      	int j;
      	int p;
      	int n;
      	int handPos;
      	int choice1;
      	int choice2;
      	int choice3;

      	struct gameState G;

      	printf ("Testing SalvagerCard:\n");

      	//exact copy from betterTestDrawCard.c
      	SelectStream(2);
      	PutSeed(3);

      	for (n = 0; n < 2000; n++) {
      	    for (i = 0; i < sizeof(struct gameState); i++) {
      	      ((char*)&G)[i] = floor(Random() * 256);
      	    }

      	// end of exact copy


      	 p = 2; //make number of players 2
      	 G.whoseTurn = p; //make whose turn be player p

      	 //make choices random
         choice1 = floor(Random() * 5);
         choice2 = floor(Random() * 5);
         choice3 = floor(Random() * 5);


      	 //set the deck count for a player to a random number
      	 G.deckCount[p] = floor(Random() * MAX_DECK);
      	 	 //go through the player deck
      	 	 for (j=0; j < G.deckCount[p]; j++){
      	 		 G.deck[p][j] = floor(Random() * 25);
      	 	 }

      	 //set the discard deck count for a player to a random number
      	 G.discardCount[p] = floor(Random() * MAX_DECK);
      	 //go through the player discard deck
      	 	 	 for (j=0; j < G.discardCount[p]; j++){
      	 	 		 G.discard[p][j] = floor(Random() * 25);
      	 	 	 }

      	 //set the hand count for a player
      	 G.handCount[p] = 5;


      	 //set the played card count for a player to a random number
      	 G.playedCardCount = floor(Random() * MAX_DECK);
      	 //go through the player hand
      	 	 	 	 for (j=0; j < G.playedCardCount; j++){
      	 	 	 	 		 G.playedCards[j] = floor(Random() * 25);
      	  	 	 	 }

      	 handPos = floor(Random() * G.handCount[p]);  //make hand position random
      	 G.hand[p][handPos] = 7; //make the card be adventurer

      	checkSalvagerCard(p, handPos, choice1, choice2, choice3, &G);
      	}


      	 printf ("ALL TESTS OK\n");

      	return 0;
      }




